<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$this->model('model')->checkuser();
if($_RQ['op']=='main'){
	$store_id=intval($_RQ['storeid']);
	$this->store_id=$store_id;
	$classid=intval($_RQ['classid']);
	$lessonid=intval($_RQ['lessonid']);
	$isfee=intval($_RQ['isfee']);
	$dayid=intval($_RQ['dayid']);
	$taocankey=intval($_RQ['taocankey']);
	if(empty($store_id)){
		exi('请先选择校区！','error','goback');
	}
	if(empty($classid) && empty($lessonid)){
		exi('请先选择班级/课程！','error','goback');
	}
	//下单页
	$item=array();
	//提交数据
	$return['post'] = array(
		'storeid'=>$store_id,
		'classid'=>$classid,
		'isfee'=>$isfee,
		'lessonid'=>$lessonid,
		'dayid'=>$dayid,
		'taocankey'=>$taocankey
	);
	$return['bodys']=pdo_getall('jy_edu_body',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['store']=pdo_get('jy_edu_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));	
	if($lessonid){
		$data=array();
		$data['0']=date("Y-m-d",time("day")); 
		$data['1']=date("Y-m-d",strtotime("+1 day")); 
		$data['2']=date("Y-m-d",strtotime("+2 day")); 
		$data['3']=date("Y-m-d",strtotime("+3 day")); 
		$data['4']=date("Y-m-d",strtotime("+4 day")); 
		$data['5']=date("Y-m-d",strtotime("+5 day")); 
		$data['6']=date("Y-m-d",strtotime("+6 day"));
		$return['lesson']=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonid));
		$return['lesson']['taocan']=json_decode($return['lesson']['taocan'],true);
		$return['day']=$data[$dayid];
		if($isfee){
			$return['lesson']['price']=0.00;
		}
		$classid=$return['lesson']['classid'];
		if($return['lesson']['istimes']==2){
			$return['lesson']['price']=$return['lesson']['taocan'][$taocankey]['price'];
			$return['lesson']['nowtaocan']=$return['lesson']['taocan'][$taocankey];
			$return['lesson']['nowtaocantitle']=$return['lesson']['nowtaocan']['times'].'课时套餐';
		}
	}
	$return['class']=pdo_get('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$classid));
	$return['group']=pdo_get('jy_edu_class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$return['class']['group_id']));
	$return['group']['diyform']=json_decode($return['group']['diyform'],true);
	set_html('title','确认订单');
	exi($return);
}

// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	//公共参数
	if(!$post['bodyid'] && (!$post['name'] || !$post['agetime'])){
		exi('请选择报名小孩或填写完整报名信息','error');
	}
	if(!$post['bodyid']){
		$data =array(
			'name'=>$post['name'],
			'agetime'=>intval(strtotime($post['agetime'])),
			'sex'=>intval($post['sex']),
			'storeid'=>$this->store_id
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=time();
		pdo_insert('jy_edu_body',$data);
		$bodyid=pdo_insertid;
	}else{
		$bodyid=intval($post['bodyid']);
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'storeid'=>intval($post['storeid']),
		'classid'=>intval($post['classid']),
		'lesson_id'=>intval($post['lessonid']),
		'worker_id'=>intval($post['worker_id']),
		'bodyid'=>$bodyid,
		'taocankey'=>intval($post['taocankey']),
		'remark'=>trim($post['remark']),
		'diyformdata'=>ijson_encode($post['diyformdata']),
		'name'=>trim($post['name']),
		'agetime'=>intval(strtotime($post['agetime'])),
		'sex'=>intval($post['sex']),
	);
	$groupid=pdo_getcolumn('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($post['classid'])),'group_id');
	if($post['isfee']){
		$new_order['money']=0;
	}else{
		if(!$new_order['lesson_id']){
			$new_order['money']=pdo_getcolumn('jy_edu_class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$groupid),'price');
		}else{
			$lesson=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$new_order['lesson_id']));
			$lesson['taocan']=json_decode($lesson['taocan'],true);
			if($lesson['istimes']==2){
				$new_order['money']=$lesson['taocan'][$post['taocankey']]['price'];
				$new_order['times']=$lesson['taocan'][$post['taocankey']]['times'];
			}else{
				$new_order['money']=$lesson['price'];
				$new_order['times']=$lesson['times'];
			}
		}
	}
	$order_id=model('jy_edu/order')->create($new_order,'',$url);
	$params=array(
		'id'=>$order_id,
		'title'=>'报名上课',
		'fee'=>$new_order['money'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_edu/create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>'jy_edu/order/detail/id='.$order_id,
		'error_url'=>'jy_edu/order/detail/id='.$order_id,
	);
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_edu_order',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_edu/order/');
	}
	$order=pdo_get('jy_edu_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_edu/order/');
	}
	if($order['pay']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['pay'].'|'.$payresult['fee'],'error','jy_edu/order/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
	);
	$url=custom_url('jy_edu/order/detail/id='.$order['id'],true);
	model('jy_edu/order')->order_pay($order['id'],$params,$url);
	exi('支付成功!');
}

?>